﻿using System;
using System.Text;
using System.Windows.Data;

namespace mal.WP7.GoogleAnalytics.Converters
{
    public class DoubleToTimeSpanConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is double)
            {
                TimeSpan duration = TimeSpan.FromSeconds((double)value);
                StringBuilder result = new StringBuilder();
                if (duration.Hours == 0)
                {
                    result.Append("00");
                }
                else if (duration.Hours <10)
                {
                    result.AppendFormat("0{0}", duration.Hours);
                }
                else
                {
                    result.AppendFormat("{0}", duration.Hours);
                }
                result.Append(":");

                if (duration.Minutes == 0)
                {
                    result.Append("00");
                }
                else if (duration.Minutes < 10)
                {
                    result.AppendFormat("0{0}", duration.Minutes);
                }
                else
                {
                    result.AppendFormat("{0}", duration.Minutes);
                }
                result.Append(":");

                if (duration.Seconds == 0)
                {
                    result.Append("00");
                }
                else if (duration.Seconds < 10)
                {
                    result.AppendFormat("0{0}", duration.Seconds);
                }
                else
                {
                    result.AppendFormat("{0}", duration.Seconds);
                }

                return result.ToString();
            }
            else
            {
                return value;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
